﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class EditAdvisor : Form
    {
        private bool bEditing = false;
        private string sEditingAdvisor = "";

        public EditAdvisor()
        {
            InitializeComponent();
        }

        public EditAdvisor(string agentCode)
        {
            sEditingAdvisor = agentCode;
            InitializeComponent();
            AcceptButton = buttonOK;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            ClientCategory cat = new ClientCategory();

            cat.Code = comboBoxAdvisorCode.Text;
            cat.Description = textBoxDesc.Text;

            try
            {
                ClientCategoryServices.Update(cat);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to update client category\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Client category information updated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private bool validateInputs()
        {
            if (textBoxDesc.Text == "")
            {
                MessageBox.Show("Enter description", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void buttonCancel_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonGO_Click(object sender, EventArgs e)
        {
            ClientCategory cat = null;
            try
            {
                cat = ClientCategoryServices.GetClientCategoryByCode(comboBoxAdvisorCode.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid category code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (cat == null)
            {
                MessageBox.Show("Invalid category code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            EnableControls(true);

            textBoxDesc.Text = cat.Description;

            bEditing = true;
            sEditingAdvisor = comboBoxAdvisorCode.Text;
        }

        private void EnableControls(bool bEnable)
        {
            textBoxDesc.Enabled = bEnable;
            buttonOK.Enabled = bEnable;
        }

        private void EditAdvisor_Load(object sender, EventArgs e)
        {
            if (sEditingAdvisor != "")
            {
                comboBoxAdvisorCode.Items.Add(sEditingAdvisor);
                comboBoxAdvisorCode.SelectedIndex = 0;
                comboBoxAdvisorCode.Enabled = false;
                bEditing = true;
                buttonGO_Click(null, null);
                return;
            }

            try
            {
                ClientCategoryCollection catCol = ClientCategoryServices.GetClientCategoryCollection();

                for (int iI = 0; iI < catCol.Count; iI++)
	            {
                    comboBoxAdvisorCode.Items.Add(catCol[iI].Code);
	            }
            }
            catch (System.Exception ex)
            {
            	
            }

            EnableControls(false);
        }

        private void comboBoxAdvisorCode_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void ClearFields()
        {
            textBoxDesc.Text = "";
        }

        private void comboBoxAdvisorCode_SelectionChangeCommitted(object sender, EventArgs e)
        {
            ClientCategory cat = null;
            try
            {
                cat = ClientCategoryServices.GetClientCategoryByCode(comboBoxAdvisorCode.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid category code  " + comboBoxAdvisorCode.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                EnableControls(false);
                bEditing = false;
                return;
            }

            if (cat == null)
            {
                MessageBox.Show("Invalid category code  " + comboBoxAdvisorCode.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                EnableControls(false);
                bEditing = false;
                return;
            }

            EnableControls(true);

            textBoxDesc.Text = cat.Description;

            bEditing = true;
            sEditingAdvisor = comboBoxAdvisorCode.Text;
        }
    }
}
